﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Check;
public partial class Insert_WebOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            InsertOrder();
        }
    }

    public int sourceType
    {
        get
        {
            if (Session["SourceType"] == null)
            {
                return 1;
            }
            return (int)(Session["SourceType"]);

        }
    }//来源(1-网页、2-软件)
    public void InsertOrder()
    {
        var paytype = Request["paytype"];
        var payid = Request["payid"];
        var payid1=Request["payid1"];
        
        double tmpamount = 0;
        double allmoney = 0;
        int allnum = 0;
        if (Session["EmoneyShopCard"] != null)
        {
            List<pay.Entity.ShopCard> list = (List<pay.Entity.ShopCard>)Session["EmoneyShopCard"];
            pay.BLL.ProductBLL productbll = new pay.BLL.ProductBLL();
            pay.Entity.Web_Order order = new pay.Entity.Web_Order();

            pay.Entity.OrderDetail[] odetails = new pay.Entity.OrderDetail[list.Count];
            order.Ordernumber = "GH" + DateTime.Now.ToString("yyyyMMddHHmmssFFF");

            for (int i = 0; i < list.Count; i++)
            {
                pay.Entity.Product product = productbll.GetElementById(list[i].ProductId);

                odetails[i] = new pay.Entity.OrderDetail();
                odetails[i].Ordernumber = order.Ordernumber;
                odetails[i].Issell = "1";
                odetails[i].Isonline = 1;
                odetails[i].Productid = list[i].ProductId;

                odetails[i].Singleprice = list[i].Price;
                odetails[i].Nowprice = list[i].Price;
                odetails[i].Num = list[i].Count;
                allnum += list[i].Count;
                tmpamount = list[i].Amount;
                allmoney += tmpamount;


            }
            if (Session["uname"] == null)
               {
                   Response.Write("<script>alert('用户姓名不能为空!'); this.history.back(-1);</script>");
                   return ;
               }

            if (Session["tel"] == null)
              {
                  Response.Write("<script>alert('电话号码不能为空!'); this.history.back(-1);</script>");
                  return;
              }
            
           // order.Tel = Session["tel"].ToString();
            string str = Session["tel"].ToString();
            if(Verification.IsMobileNum(str)==true || Verification.IsPhoneNum(str)==true)
            {
                order.Mobile = str.Substring(0, 3) + "****" + str.Substring(7, 4);
            }
            else
            {
                Response.Write("<script>alert('请输入正确的电话或手机号码,例如:13916752109或0712-3614072'); this.history.back(-1);</script>");
                return;
            }
            

            order.guid = str;
           
            string reg_name = Session["uname"].ToString().Trim().Replace(" ", "");
            if (Verification.IsTszfc(reg_name) == false)
            {
                Response.Write("<script>alert('用户姓名包含非法字符，请重新输入!'); this.history.back(-1);</script>");
                return;
            }
            
            if (Session["Linkman"] == null)
            {
                return;
            }

            if (Session["ProductUserId"] != null)
            {
              
              order.Username = Session["ProductUserId"].ToString();
              order.Truename = reg_name;
            }
            else
            {
                order.Truename = reg_name;

            }

           if(paytype!=null)
           {
               order.Paytypeid = Convert.ToInt32(Request["paytype"]);
           }
            order.LinkMan = Session["Linkman"].ToString();
            order.SourceType = sourceType; //来源(1-网页,2-软件)
            order.Orderdate = DateTime.Now;

            order.Status = 2; //未付款

            order.Allnum = allnum;
            order.Allmoney = allmoney;
            if (Request["email"] != null)
            {
                order.Email = Request["email"];
            }
            
            pay.BLL.OrderBLL orderBll = new pay.BLL.OrderBLL();
            string ok_url = "";
            if (orderBll.Add(order, odetails))
            {

                switch(paytype)
                {   
                    //0-快钱;2-支付宝; 11-网银; 10-快钱信用卡; 12-手机语音 13-手机钱包
                    case "0":
                        ok_url = "kuaiqian/send.aspx?ordernum=" + order.Ordernumber + "";
                      
                        break;
                   
                    case "2":
                        ok_url = "zhifubao/AliSend.aspx?ordernum=" + order.Ordernumber + "";
                        break;

                    case "11":
                        string Bankid = Request["bankid"];
                        Session["Bankid"] = Bankid;
                        ok_url = "wangyin/send.aspx?ordernum=" + order.Ordernumber + "";
                        break;

                    case "10":
                        ok_url = "xinyongka/send.aspx?ordernum=" + order.Ordernumber + "";
                        break;

                    case "12":
                        ok_url = "shoujiyuyin/send.aspx?ordernum=" + order.Ordernumber + "";
                        break;

                    case "13":
                        ok_url = "shoujiqianbao/webPay.aspx?ordernum=" + order.Ordernumber + "";
                        break;

                    default:
                        ok_url = "kuaiqian/send.aspx?ordernum=" + order.Ordernumber + "";
                        break;
                }
                Response.Redirect(ok_url);
            }
           else
           {
                    Response.Write("<script>alert('false'); this.history.back(-1);</script>");
           }
        }


    }
}